<template>
  <div class="login-container">

    <div class="front">
      <el-card class="card">
        <el-form ref="loginForm" style="background-color: #f1f1f1" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
          <div class="title-container">
            <h3 class="title">{{ $t('login.title') }}</h3>
            <lang-select v-if="setting.langSelect" class="set-language" />
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              :placeholder="$t('login.plUserName')"
              name="username"
              size="small"
              type="text"
              tabindex="1"
              autocomplete="off"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :class="passwordType"
                :placeholder="$t('login.plPassword')"
                name="password"
                size="small"
                tabindex="2"
                auto-complete="off"
                autocomplete="new-password"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'no-autofill-pwd' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ $t('login.logIn') }}</el-button>
        </el-form>
      </el-card>
    </div>

  </div>
</template>

<script>
import { isLoginUsername, isLoginPassword } from '@/utils/verify'
import LangSelect from '@/components/LangSelect/index.vue'
import settings from '@/settings'

export default {
  name: 'Login',
  components: { LangSelect },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isLoginUsername(value)) {
        callback(new Error(this.$t('common.ruleMsg.loginUserNameErr')))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!isLoginPassword(value)) {
        callback(new Error(this.$t('common.ruleMsg.loginPasswordErr')))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'no-autofill-pwd',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  computed: {
    setting() {
      return settings
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'no-autofill-pwd') {
        this.passwordType = ''
      } else {
        this.passwordType = 'no-autofill-pwd'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(async() => {
              let loginPath = this.redirect || '/'
              if (['/404', '/401', '/'].includes(loginPath)) {
                loginPath = '/'
              }
              this.$router.push({ path: loginPath, query: this.otherQuery })
              await this.$store.dispatch('user/setLoginPath', loginPath)
              this.loading = false
            })
            .catch((e) => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */

$bg:#283443;
$light_gray:#fff;
// $light_gray:#0c0c0c;
$light_gray:#282829;

$cursor: #fff;
$cursor:#282829;

// $cursor: white;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 32px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      // color:white;
      height: 32px;
      font-size: 15px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    border: 1px solid #787a7c;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    // color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
// $bg:#e7eaee;
$dark_gray:#889aa4;
$light_gray:#eee;
// $light_gray:#0c0c0c;
$light_gray:#282829;
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url("../../../public/login-background.jpg");
  // background-color: $bg;
  background-size: cover;
  overflow: hidden;

  .login-form {
    position: relative;
    // width: 440px;
    max-width: 400px;
    padding: 35px 35px;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: $light_gray;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

::v-deep .el-form-item--medium .el-form-item__content {
  line-height: 32px;
}

::v-deep .el-button--medium {
    font-size: 18px;
}

.front {
  z-index: 1;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #ffffff;
}
.card {
  width: 400px;
  background-color: rgba(
    200,
    200,
    200,
    0.3
  ); /**rgba中的a为alpha通道， 为不透明参数，.0即为完全透明*/
  border-color: rgba(200, 200, 200, 0.5);
  border-radius: 18px;
  z-index: 2;
}

::v-deep .el-card__body {
  padding: 10px 10px;
}
</style>
